<?php

	class Product extends CI_Model
	{
		function __construct()
		{
			parent::__construct();
		}
				
		function getProduct($id = false)
		{
			$query = $this->db->get_where('products',array('id'=>$id));
			return $query->row_array();
		}

		function getAllProducts($num_of_products)
		{
			if($num_of_products)
			{
				$query = $this->db->get('products',5,$num_of_products);
			}
			else
			{
				$query = $this->db->get('products',5,0);				
			}
			
			return $query->result_array();
		}

		function getNewProducts()
		{
			$this->db->select('*');
			$this->db->from('products');
			$this->db->order_by('id','desc');
			$this->db->limit(9);

			$query = $this->db->get();
			return $query->result_array();
		}
		
		function getNewProducts1($cat_id = false,$num = false)
		{
			$this->db->select('*');
			$this->db->from('products');
			$this->db->where(array('category_id'=>$cat_id));
			//$this->db->order_by('id','desc');			
			$this->db->limit($num);

			$query = $this->db->get();
			return $query->result_array();
		}

		function addNewProduct($data)
		{
			return $this->db->insert('products',$data);	
		}

		function editProduct($data = false,$id = false)
		{
			$this->db->where('id',$id);
			$this->db->update('products',$data);
		}

		function search($data = false,$page = false)
		{
			//search keyword,price,price to
			$this->db->select('*');
			$this->db->from('products');
			if(isset($data['search']['keyword']) && !empty($data['search']['keyword']))
			{
				$this->db->like('name',$data['search']['keyword']);	
			}
			if(isset($data['search']['price']) && !empty($data['search']['price']))
			{
				$this->db->where('price >',$data['search']['price']);
			}
			if(isset($data['search']['price_to']) && !empty($data['search']['price_to']))
			{
				$this->db->where('price <',$data['search']['price_to']);	
			}

			if($page)
			{
				$this->db->limit(10,$page);
			}
			else
			{
				$this->db->limit(10,0);
			}
			
			$query = $this->db->get();
			
			return $query->result_array();
		}
	}